<template>
  <a-row style="padding: 10px;">
    <a-col span="24">
      <a-form layout="inline">
        <a-form-item label="租户默认开通应用">
          <a-select mode="multiple" v-model="defaultOption.apps" @change="changeApps" style="width: 195px"
                    placeholder="请选择租户默认开通应用">
            <a-select-option v-for="appItem in appList" :key="appItem.appCode">{{appItem.appName}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="租户管理员默认角色">
          <a-select mode="multiple" v-model="defaultOption.roles" style="width: 195px"
                    placeholder="请选择租户默认角色">
            <a-select-option v-for="role in roleList" :key="role.roleCode">{{role.roleName}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="setDefaultApp">保存</a-button>
        </a-form-item>
      </a-form>
    </a-col>
  </a-row>
</template>

<script>
  export default {
    name: 'Config',
    data () {
      return {
        pageObj: {total: 10, pageSize: 10, pageNum: 1, pageSizeOptions: ['5', '10', '20', '50', '100']},
        loading: false,
        configList: [],
        appList: [],
        selectAppIdList: [],
        roleList: [],
        defaultOption: {
          apps: [],
          appConfigId: null,
          roles: [],
          roleConfigId: null
        }
      }
    },
    mounted () {
      this.getConfigList()
    },
    methods: {
      // 查询配置列表
      getConfigList () {
        this.loading = true
        this.http.post('upmsApi', '/admin/config/getConfigList', {
          pageNum: this.pageObj.pageNum,
          pageSize: this.pageObj.pageSize,
          data: {}
        }).then((res) => {
          if (res.success) {
            this.configList = res.rows
            this.pageObj.total = res.total
            this.handleConfig()
            this.getAppList()
          }
          this.loading = false
        })
      },
      // 获取默认配置信息
      handleConfig () {
        this.configList.forEach(config => {
          if (config.configCode === 'DEFAULT_APPS') {
            this.defaultOption.apps = config.configValue.split(',')
            this.defaultOption.appConfigId = config.configId
          }
          if (config.configCode === 'DEFAULT_ROLES') {
            this.defaultOption.roles = config.configValue.split(',')
            this.defaultOption.roleConfigId = config.configId
          }
        })
        console.log(this.defaultOption)
      },
      // 查询应用列表
      getAppList () {
        this.http.post('upmsApi', '/app/getAppList', {
          pageNum: this.pageObj.pageNum,
          pageSize: -1,
          data: {}
        }).then((res) => {
          if (res.success) {
            this.appList = res.rows
            this.appList.forEach(app => {
              if (this.defaultOption.apps.indexOf(app.appCode) > -1) {
                this.selectAppIdList.push(app.appId)
              }
            })
            this.getAppRoleList()
          }
        })
      },
      // 查询应用预制角色
      getAppRoleList () {
        this.http.post('upmsApi', '/appRole/getRoleListByAppId', {
          pageSize: -1,
          longList: this.selectAppIdList,
          data: {}
        }).then((res) => {
          if (res.success) {
            this.roleList = res.rows
          }
        })
      },
      // 修改默认应用事件
      changeApps (value) {
        console.log(value)
        this.selectAppIdList = []
        this.appList.forEach(app => {
          if (value.indexOf(app.appCode) > -1) {
            this.selectAppIdList.push(app.appId)
          }
        })
        this.getAppRoleList()
      },
      // 设置默认值
      setDefaultApp () {
        this.http.post('upmsApi', '/admin/config/updateConfig', {
          configId: this.defaultOption.appConfigId,
          configValue: this.defaultOption.apps.join(',')
        }).then((res) => {
          if (res.success) {
            this.http.post('upmsApi', '/admin/config/updateConfig', {
              configId: this.defaultOption.roleConfigId,
              configValue: this.defaultOption.roles.join(',')
            }).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
